package com.lw.system.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lw.common.basemodel.EasyuiModel;
import com.lw.common.basemodel.Json;
import com.lw.common.core.service.BaseServiceI;
import com.lw.common.util.ModelConvertUtil;
import com.lw.common.web.controller.BaseController;
import com.lw.system.model.User;
import com.lw.system.service.UserService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController<User, String> {
	@Resource
	private UserService userService;

	@Override
	public BaseServiceI<User, String> getBaseService() {
		return userService;
	}

	@ApiOperation(value = "显示用户", httpMethod = "GET", notes = "显示用户")
	@RequestMapping(value = "/{id}/showUser", method = RequestMethod.GET)
	public User showUser(@PathVariable String id) {
		User u = userService.selectByPrimaryKey(id);
		return u;
	}

	@ApiOperation(value = "hibernate原生验证测试", httpMethod = "GET", notes = "显示用户")
	@RequestMapping(value = "/testUser", method = RequestMethod.GET)
	public Json showUser(@Valid User user, BindingResult result) {
		if (result.hasErrors()) {
			return setFailed(result.getAllErrors().get(0).getDefaultMessage());
		}
		return setSimpleSuccess(user);
	}

	@ApiOperation(value = "spring验证测试", httpMethod = "GET", notes = "显示用户")
	@RequestMapping(value = "/testUser2", method = RequestMethod.GET)
	public Json showUser2(@Validated User name) {
		return setSimpleSuccess(name);
	}

	@RequestMapping(value = "/findUserAndRolesByPage", method = RequestMethod.GET)
	public EasyuiModel<User> findUserAndRolesByPage(User u, int page, int rows) {
		return ModelConvertUtil.pageInfo2EasyuiModel(userService.findUserAndRoles(u, page, rows));
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Json edit(User user) {
		return userService.edit(user) > 0 ? setSimpleSuccess(user) : setFailed();
	}

}
